<template>
  <div class="order">
    <div class="order__price">
      实付金额<span class="order__price__num"
        >￥{{ caculation.totalPrice }}</span
      >
    </div>
    <div class="order__btn" @click="() => showConfirmMask(true)">提交订单</div>
  </div>
  <div class="mask" v-if="showConfirm" @click="() => showConfirmMask(false)">
    <div class="mask__content" @click.stop>
      <h4 class="mask__title">确认要离开收银台吗？</h4>
      <p class="mask__desc">请尽快完成支付，否则将被取消</p>
      <div class="mask__btns">
        <div class="mask__btns__cancel" @click="() => showConfirmMask(false)">
          取消
        </div>
        <div
          class="mask__btns__submit"
          @click="() => handleConfirmOrder(false)"
        >
          确认支付
        </div>
      </div>
    </div>
  </div>
  <Toast v-if="toastData.showToast" :msg="toastData.toastMsg" />
</template>
<script>
import { ref } from "vue";
import { useCommonCartEffect } from "../../effect/cartEffects";
import { useRoute, useRouter } from "vue-router";
import { post } from "../../utils/request";
import Toast, { useToastEffect } from "../../components/Toast";
import { useStore } from "vuex";
const useMakeOrderEffect = (shopId) => {
  const router = useRouter();
  const store = useStore();
  const { shopName, productList } = useCommonCartEffect(shopId);
  const { toastData, showToast } = useToastEffect();
  const handleConfirmOrder = async (isCanceled) => {
    // 提交订单
    const products = [];
    for (const i in productList.value) {
      products.push({
        id: parseInt(productList.value[i]._id),
        num: parseInt(productList.value[i].count),
      });
    }
    try {
      const result = await post("/api/order", {
        addressId: 1,
        shopId,
        shopName: shopName.value,
        isCanceled,
        products,
      });
      if (result.data.errno === 0) {
        store.commit("handleClearCart", { shopId });
        router.push({ name: "OrderList" });
      } else {
        showToast("最后的接口有问题,下单失败");
      }
    } catch (e) {
      showToast("baseUrl有问题,下单失败");
    }
  };
  return { handleConfirmOrder, toastData, showToast };
};
const useShowMaskEffect = () => {
  const showConfirm = ref(false);
  const showConfirmMask = (showConfirmStatus) => {
    showConfirm.value = showConfirmStatus;
  };
  return { showConfirmMask, showConfirm };
};
export default {
  name: "Order",
  components: { Toast },
  setup() {
    const route = useRoute();
    const shopId = parseInt(route.params.id);
    const { handleConfirmOrder, toastData, showToast } =
      useMakeOrderEffect(shopId);
    const { caculation } = useCommonCartEffect(shopId);
    const { showConfirmMask, showConfirm } = useShowMaskEffect();
    return {
      caculation,
      handleConfirmOrder,
      toastData,
      showToast,
      showConfirm,
      showConfirmMask,
    };
  },
};
</script>
<style lang="scss" scoped>
@import "../../style/mixins.scss";
@import "../../style/viriables.scss";
.order {
  overflow: hidden;
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
  height: 0.5rem;
  background-color: #fff;
  &__price {
    width: 100%;
    margin-left: 0.18rem;
    font-size: 0.14rem;
    line-height: 0.5rem;
    color: #333;
    &__num {
      margin-left: 0.05rem;
      font-weight: 600;
    }
  }
  &__btn {
    position: absolute;
    top: 0;
    right: 0;
    width: 0.98rem;
    line-height: 0.5rem;
    text-align: center;
    font-size: 0.14rem;
    color: #fff;
    background-color: $blue-color;
  }
}
.mask {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
  .mask__content {
    box-sizing: border-box;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    width: 3rem;
    height: 1.56rem;
    text-align: center;
    background-color: #fff;
    border-radius: 0.04rem;
    .mask__title {
      margin: 0;
      margin-top: 0.14rem;
      font-size: 0.18rem;
      line-height: 0.25rem;
    }
    .mask__desc {
      font-size: 0.14rem;
    }
    .mask__btns {
      display: flex;
      margin: 0.24rem 0.58rem 0 0.58rem;
      .mask__btns__cancel,
      .mask__btns__submit {
        box-sizing: border-box;
        flex: 1;
        width: 0.8rem;
        line-height: 0.32rem;
        font-size: 0.14rem;
        border: 0.01rem solid $blue-color;
        border-radius: 0.16rem;
      }
      .mask__btns__cancel {
        margin-right: 0.3rem;
        color: $blue-color;
      }
      .mask__btns__submit {
        color: #fff;
        background-color: $blue-color;
      }
    }
  }
}
</style>
